package org.lan.permission.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import org.lan.permission.dao.IRolePermissionDao;
import org.lan.permission.dao.IUserDao;
import org.lan.permission.dao.IUserRoleDao;
import org.lan.permission.entity.Permission;
import org.lan.permission.entity.Role;
import org.lan.permission.entity.User;
import org.lan.permission.service.IUserService;
import org.springframework.stereotype.Service;

@Service
public class UserService implements IUserService {

	private IUserDao userDao;

	@Inject
	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	private IUserRoleDao userRoleDao;

	@Inject
	public void setRolePermissionDao(IRolePermissionDao rolePermissionDao) {
		this.rolePermissionDao = rolePermissionDao;
	}

	private IRolePermissionDao rolePermissionDao;

	@Inject
	public void setUserRoleDao(IUserRoleDao userRoleDao) {
		this.userRoleDao = userRoleDao;
	}

	@Override
	public List<String> getUrlsByUsername(String useranme) {
		List<String> urls = null;
		List<Role> roles = userRoleDao.findByUsername(useranme);
		if (roles != null && roles.size() > 0) {
			urls = new ArrayList<String>();
			for (Role role : roles) {
				List<Permission> permissions = rolePermissionDao.findByRoleId(role.getId());
				if (permissions != null && permissions.size() > 0) {
					for (Permission permission : permissions) {
						urls.add(permission.getUrl());
					}
				}
			}
		}
		return urls;
	}

	@Override
	public User login(String useranme, String password) {
		return userDao.findByUsernameAndPassword(useranme, password);
	}

}
